package ym.controller.repository;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import ym.pojo.ProductRecord;
import ym.service.ProductRecordService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 营养产品记录
 */
@CrossOrigin
@RestController
@RequestMapping("/front/productRecord")
public class FrontProductRecordCon {

    @DubboReference
    private ProductRecordService productRecordService;

    /**
     * 新增
     *
     * @param productRecord
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(ProductRecord productRecord) {
        productRecordService.add(productRecord);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param productRecord
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(ProductRecord productRecord) {
        productRecordService.update(productRecord);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param productRecordId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer productRecordId) {
        productRecordService.delete(productRecordId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param productRecordId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer productRecordId) {
        ProductRecord productRecord = productRecordService.findById(productRecordId);
        return BaseResult.ok(productRecord);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<ProductRecord> all = productRecordService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    public BaseResult search(String search, int page, int size) {
        Page<ProductRecord> productRecordPage = productRecordService.search(search, page, size);
        return BaseResult.ok(productRecordPage);
    }

    /**
     * 根据产品id分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/searchByNutritionProductId")
    public BaseResult searchByNutritionProductId(Integer nutritionProductId, String search, int page, int size) {
        Page<ProductRecord> productRecordPage = productRecordService.searchByNutritionProductId(nutritionProductId, search, page, size);
        return BaseResult.ok(productRecordPage);
    }

}
